//
//  HeaderView.m
//  licai
//
//  Created by apple on 15/11/16.
//  Copyright © 2015年 Boye. All rights reserved.
//

#import "ByPersonalHeaderView.h"

#import "ByLoginVC.h"           // 登录
#import "ByPersonalDataVC.h"    // 资料
@implementation ByPersonalHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        [self _initViews];
        
    }
    
    return self;
    
}

#pragma mark -- 初始化子视图
- (void)_initViews {
    [self _initBgImgView];
    [self _initIconImgView];
    [self _initNickLabel];
}
- (void)_initBgImgView {
    self.bgImgView = [[UIImageView alloc] initWithFrame:self.bounds];
    self.bgImgView.backgroundColor = JM_THEME_COLOR;
    [self addSubview:self.bgImgView];
}
- (void)_initIconImgView {
    self.iconImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 80, 80)];
    self.iconImgView.center = CGPointMake(SCREEN_WIDTH / 2.0, self.iconImgView.center.y);
    self.iconImgView.userInteractionEnabled = YES;
    self.iconImgView.backgroundColor = [UIColor grayColor];
    // 切圆角
    [BoyeViewHandle cutViewConner:self.iconImgView radius:self.iconImgView.width / 2.0];
    [self addSubview:self.iconImgView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iconTap:)];
    [self.iconImgView addGestureRecognizer:tap];
}
- (void)_initNickLabel {
    self.nickLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.iconImgView.bottom + 15, SCREEN_WIDTH / 2.0, 20)];
    self.nickLabel.center = CGPointMake(SCREEN_WIDTH / 2.0, self.nickLabel.center.y);
    self.nickLabel.font = FONT(14);
    self.nickLabel.textAlignment = NSTextAlignmentCenter;
    self.nickLabel.textColor = [UIColor whiteColor];
    self.nickLabel.text = @"朱凯";
    [self addSubview:self.nickLabel];
}

#pragma mark -- 点击事件
- (void)iconTap:(UITapGestureRecognizer *)tap {
    
    if (BY_USER) { // 已经登录了，则进入个人资料界面
        ByPersonalDataVC *vc = [[ByPersonalDataVC alloc] init];
        [self.viewController.navigationController pushViewController:vc animated:YES];
    } else { // 未登录则去登陆
        ByLoginVC *vc = [[ByLoginVC alloc] init];
        ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
        [self.viewController presentViewController:navi animated:YES completion:nil];
    }
}



#pragma mark -- 其他方法
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *view = [super hitTest:point withEvent:event];
    
    point = [self convertPoint:point toView:self.iconImgView];
    if ([self.iconImgView pointInside:point withEvent:event]) {
        return self.iconImgView;
    }
    
    return view;
}

@end
